`
whether to continue running a block of code or not, so being able to construct them
is fundamental to bash programming.
There are multiple kinds of test operators. File test operators allow us to
perform tests against files on the filesystem, such as checking if a file is executable
or if some directory exists. Table 2-1 shows a short list of the available tests.
Table 2-1
File Test Operators
Operator
Description
-d FILE
Checks whether the file is a directory
-r FILE
Checks whether the file is readable
-x FILE
Checks whether the file is executable
-w FILE
Checks whether the file is writable
-f FILE
Checks whether the file is a regular file
-s FILE
Checks whether the file size is greater than zero
You can find the full list of file test operators at
https://ss64.com/bash/test.html.
String comparison operators allow us to perform tests related to strings, such
as testing whether one string is equal to another. Table 2-2 shows the string
comparison operators.
Table 2-2
String Comparison Operators
Operator
Description
=
Checks whether a string is equal to another string
==
Synonym of = when used within [[ ]] constructs
!=
Checks whether a string is not equal to another string
<
Checks whether a string comes before another string (in alphabetical
order)
>
Checks whether a string comes after another string (in alphabetical
order)
-z
Checks whether a string is null
-n
Checks whether a string is not null
Integer comparison operators allow us to perform checks on integers, such as
if an integer is less than or greater than another. Table 2-3 shows the available
operators.
Table 2-3
Integer Comparison Operators
Operator
Description
-eq
Checks whether a number is equal to another number
-ne
Checks whether a number is not equal to another number
-ge
Checks whether a number is greater than or equal to another number
-gt
Checks whether a number is greater than another number
-lt
Checks whether a number is less than another number
-le
Checks whether a number is less than or equal to another number
Let’s use these operators in flow-control mechanisms to decide what code to
run next.
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks